package com.tripcube.system.controller;

import com.tripcube.common.core.domain.R;
import com.tripcube.common.core.web.controller.BaseController;
import com.tripcube.common.core.web.domain.AjaxResult;
import com.tripcube.common.core.web.page.TableDataInfo;
import com.tripcube.common.log.annotation.Log;
import com.tripcube.common.log.enums.BusinessType;
import com.tripcube.system.api.RemoteFileService;
import com.tripcube.system.api.domain.SysFile;
import com.tripcube.system.domain.SysUserCheckins;
import com.tripcube.system.domain.dto.TravelCheckinDTO;
import com.tripcube.system.domain.vo.CheckinVO;
import com.tripcube.system.service.SysUserCheckinsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/checkins")
public class SysUserCheckinsController extends BaseController {
    @Autowired
    SysUserCheckinsService userCheckinsService;
    @Autowired
    private RemoteFileService remoteFileService;
    /**
     * 用户签到
     * @param dto 签到信息
     */
    @PostMapping("/checkin")
    public AjaxResult checkin(@RequestBody TravelCheckinDTO dto) {
        Long checkinId = userCheckinsService.checkin(dto);
        return AjaxResult.success("签到成功", checkinId);
    }

    /**
     * 检查用户今日是否已签到
     * @param userId 用户ID
     * @return 是否已签到
     */
    @GetMapping("/checkin/today/{userId}")
    public AjaxResult checkinToday(@PathVariable Long userId) {
        boolean result = userCheckinsService.checkinToday(userId);
        return AjaxResult.success(result);
    }


    /**
     * 获取用户连续签到天数
     * @param userId 用户ID
     * @return 连续签到天数
     */
    @GetMapping("/continuous/{userId}")
    public AjaxResult continuousDays(@PathVariable Long userId) {
        Long days = userCheckinsService.continuousDays(userId);
        return AjaxResult.success(days);
    }


    /**
     * 旅行地打卡
     * @param dto 打卡信息
     * @return 打卡ID
     */
    @PostMapping("/travel")
    @Log(title = "打卡", businessType = BusinessType.INSERT)
    public AjaxResult travelCheckin(@RequestPart("travelCheckinDTO") TravelCheckinDTO dto, @RequestPart(name = "travelFiles", required = false) MultipartFile[] files)
    {
        if(files != null){
            R<List<SysFile>> uploads = remoteFileService.uploads(files);
            dto.setPhotos(uploads.getData().stream().map(SysFile::getUrl).toArray(String[]::new));
        }
        Long checkinId = userCheckinsService.travelCheckin(dto);
        return AjaxResult.success("打卡成功", checkinId);
    }


    /**
     * 获取附近打卡地点
     * @param lat 纬度
     * @param lng 经度
     * @param meter 距离范围(米)
     * @return 打卡地点列表
     */
    @GetMapping("/nearby")
    public List<CheckinVO> nearby(@RequestParam BigDecimal lat, BigDecimal lng, int meter) {
        return userCheckinsService.nearby(lat, lng, meter);
    }

    /**
     * 分页查询用户打卡记录
     * @param checkins 查询参数
     * @return 打卡记录分页数据
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUserCheckins checkins) {
        startPage();
        List<SysUserCheckins> list = userCheckinsService.selectCheckinsList(checkins);
        return getDataTable(list);
    }

    /**
     * 获取用户上月获得的积分总和
     * @param userId 用户ID
     * @return 积分总和
     */
    @GetMapping("/sumPointsLastMonth/{userId}")
    public AjaxResult sumPointsLastMonth(@PathVariable Long userId) {
        int points = userCheckinsService.sumPointsLastMonth(userId);
        return AjaxResult.success("查询成功",points);
    }

    /**
     * 撤销打卡记录
     * @param checkinId 打卡记录ID
     */
    @DeleteMapping("/revoke/{checkinId}")
    @Log(title = "打卡", businessType = BusinessType.DELETE)
    public AjaxResult revoke(@PathVariable Long checkinId) {
        userCheckinsService.revoke(checkinId);
        return AjaxResult.success("撤销成功");
    }

    /**
     * 恢复已撤销的打卡记录
     * @param checkinId 打卡记录ID
     */
    @PutMapping("/restore/{checkinId}")
    @Log(title = "打卡", businessType = BusinessType.UPDATE)
    public AjaxResult restore(@PathVariable Long checkinId) {
        userCheckinsService.restore(checkinId);
        return AjaxResult.success("恢复成功");
    }

}